package GUI.Cliente;

import java.rmi.RemoteException;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;

import GUI.Administracion.Exceptions.ExcepcionesMenuVentas;
import client.entities.ClientVO;
import client.exceptions.BadClientException;
import client.exceptions.IncompleteClientException;
import client.exceptions.MissingClientException;

import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

import dbExceptions.DataBaseErrorException;
import service.ServiceFacade;
import service.article.exceptions.ProxyException;
import service.client.interfaces.ClientProxyMgt;

public class ModificarAtributo extends Dialog {

	protected Object result;
	protected Shell shell;
	private Text text;
	ClientVO c;
	String[]d;
	int op;
	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public ModificarAtributo(Shell parent, int style,String[] datos, ClientVO cliente, int opcion) {
		super(parent, style);
		setText("Modificar Datos ");
		c=cliente;
		d=datos;
		op=opcion;
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), getStyle());
		shell.setSize(450, 193);
		shell.setText(getText());
		
		Label lblNewLabel = new Label(shell, SWT.NONE);
		lblNewLabel.setBounds(39, 80, 132, 14);
		lblNewLabel.setText(d[2]);
		
		text = new Text(shell, SWT.BORDER);
		text.setBounds(191, 77, 126, 19);
		
		Label lblNewLabel_1 = new Label(shell, SWT.NONE);
		lblNewLabel_1.setBounds(39, 49, 132, 14);
		lblNewLabel_1.setText(d[0]);
		
		Label lblNewLabel_2 = new Label(shell, SWT.NONE);
		lblNewLabel_2.setBounds(191, 49, 126, 14);
		lblNewLabel_2.setText(d[1]);
		
		Button btnNewButton = new Button(shell, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(op==0){
					c.setName(text.getText());
					ClientProxyMgt cl = ServiceFacade.getInstance().getClientProxyMgt();
					try {
						cl.updateClient(c);
					} catch (RemoteException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (ProxyException e1) {
						ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
						ventana.open();
						shell.dispose();
						e1.printStackTrace();
					} catch (IncompleteClientException e1) {
						ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
						ventana.open();
						shell.dispose();
					} catch (MissingClientException e1) {
						ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
						ventana.open();
						shell.dispose();
					} catch (BadClientException e1) {
						ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
						ventana.open();
						shell.dispose();
					} catch (DataBaseErrorException e1) {
						ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con la Base de Datos");
						ventana.open();
						shell.dispose();
					}
					DatosCliente d = new DatosCliente (shell, shell.getStyle(), c, 1);
					shell.setVisible(false);
					d.open();
				}
				if(op==1){
					c.setLastName(text.getText());
					DatosCliente d = new DatosCliente (shell, shell.getStyle(), c, 1);
					shell.setVisible(false);
					d.open();
				}
				if(op==2){
					c.setClientId(Integer.parseInt(text.getText()));
					DatosCliente d = new DatosCliente (shell, shell.getStyle(), c, 1);
					shell.setVisible(false);
					d.open();
				}
				if(op==3){
					c.setAddress(text.getText());
					DatosCliente d = new DatosCliente (shell, shell.getStyle(), c, 1);
					shell.setVisible(false);
					d.open();
				}
				if(op==4){
					c.setTel(text.getText());
					DatosCliente d = new DatosCliente (shell, shell.getStyle(), c, 1);
					shell.setVisible(false);
					d.open();
				}
				if(op==5){
					c.setCel(text.getText());
					DatosCliente d = new DatosCliente (shell, shell.getStyle(), c, 1);
					shell.setVisible(false);
					d.open();
				}
				if(op==6){
					c.setBirthdate(text.getText());
					DatosCliente d = new DatosCliente (shell, shell.getStyle(), c, 1);
					shell.setVisible(false);
					d.open();
				}
			}
		});
		btnNewButton.setBounds(319, 133, 94, 28);
		btnNewButton.setText("Aceptar y Modificar");

	}

}
